package com.untrance.shop.web.controller;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.untrance.shop.common.utils.JpaUtils;
import com.untrance.shop.common.utils.PagerUtils;
import com.untrance.shop.common.vo.CategoryVO;
import com.untrance.shop.common.vo.CommentVO;
import com.untrance.shop.common.vo.PagerParametersVO;
import com.untrance.shop.common.vo.ProductDetailsVO;
import com.untrance.shop.core.service.CategoryService;
import com.untrance.shop.core.service.CommentService;
import com.untrance.shop.core.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author eugene zadyra
 */
@Controller
public class ProductController {

	private static Logger log = LoggerFactory.getLogger(ProductController.class);

	@Autowired
	ProductService productService;
	@Autowired
	CategoryService categoryService;
	@Autowired
	CommentService commentService;

	@RequestMapping(value = "/product.html", method = RequestMethod.GET)
	public ModelAndView getProducts(@RequestParam(value = "id", required = true) String id,
									@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
									@RequestParam(value = "per_page", required = false, defaultValue = "12") Integer perPage) {
		ModelAndView mav = new ModelAndView("product");
		Key productKey = JpaUtils.getKey(id);
		ProductDetailsVO vo = productService.getProductDetailsVO(productKey);
		if (vo == null) {
			return CommonController.forward404;
		}
		mav.addObject("product", vo);
		List<CategoryVO> categoryChain = categoryService.getCategoriesVOChainByKey(JpaUtils.getKey(vo.getCategoryKey()));
		mav.addObject("categoryChain", categoryChain);
		List<CategoryVO> categoriesVO = categoryService.getCategoriesVOTreeByKey(JpaUtils.getKey(vo.getCategoryKey()));
		mav.addObject("categories", categoriesVO);
		List<CommentVO> commentsVO = commentService.getCommentsRelatedToProduct(productKey, page, null);
		mav.addObject("comments", commentsVO);
		Integer count = commentService.getCommentsCountForProduct(productKey);
		PagerParametersVO pagerData = PagerUtils.calculatePagerParameters(page, count, perPage);
		mav.addObject("pagerData", pagerData);
		mav.addObject("product_id", id);
		return mav;
	}

//	@RequestMapping(value = "/products.json", headers="Accept=*/*", method = RequestMethod.GET)
//	public @ResponseBody
//	ProductVOWrapper getProductsJSON(@RequestParam(value = "page", required = false) Integer page,
//			@RequestParam(value = "count", required = false) Integer count,
//			@RequestParam(value = "per_page", required = false) Integer perPage) {
//		if (page == null) {
//			page = 0;
//		}
//		if (perPage == null) {
//			perPage = 20;
//		} else if (perPage > 100) {
//			perPage = 100;
//		}
//		if (count == null) {
//			count = productService.getProductsCount();
//		}
//		Integer from = PagerUtils.calculateFirstElement(page, perPage, count);
//		List<Product> list = productService.getLimitedAmountOfProducts(from, perPage);
//		List<ProductVO> listVO = new ProductVOBuilder().createVOList(list);
//
//		return new ProductVOWrapper(listVO, count, page, perPage);
//	}

	// @RequestMapping("/admin/add-product.html")
	// public ModelAndView addProduct(@RequestParam(value = "file", required =
	// false) CommonsMultipartFile file) {
	// if (file != null && !file.isEmpty()) {
	// try {
	// imageService.saveImage(file.getName(), file.getBytes());
	// } catch (MultipleResultsException e) {
	// log.warn(e.getMessage(), e);
	// return CommonController.redirect500;
	// }
	// }
	// return new ModelAndView("redirect:/images/" + file.getName());
	// }
	//
	// @RequestMapping("/admin/remove-product.html")
	// public ModelAndView removeProduct(@RequestParam(value = "id", required =
	// true) Long id) {
	//
	// return new ModelAndView("redirect:/images/");
	// }
}
